<#import "../tpl/pageTep.ftl" as page>
<@page.pageBase currentMenu="商品分类">
<style>
    .button-success, .button-danger {
        margin-left: 5px;
    }

    .row p {
        text-align: center;
        font-size: 12px;
        color: #999;
        -webkit-transform-origin-x: 0;
        -webkit-transform: scale(0.85);
    }

    .form-horizontal .controls {
        line-height: 40px;
        height: 40px;
    }

    .button-primary, .button-success, .button-danger, .shelves-button {
        margin-left: 5px;
    }

    .form-horizontal .control-group-img {
        height: 40px;
    }

    .webuploader-pick {
        background: none;
        padding: 0;
    }
</style>
<!--suppress ALL -->
<form id="searchForm" class="form-panel" action="${basepath}/rest/manage/catalog/loadData">
    <ul class="panel-content">
        <li>
            <div class="control-group span5">
                <div class="controls">
                    <input type="text" id="name" name="name" placeholder="输入关键字搜索"/>
                </div>
            </div>
            <div class="form-actions span8">
                <button type="submit" class="button  button-primary">
                    查询
                </button>
                <a href="javascript:add()" class="button button-success">
                    新增
                </a>
            </div>
        </li>
    </ul>
</form>
<#----------------------------------------------新增表单 start---------------------------------------------------------->
<div id="addContent" style="display:none;">
    <form id="addForm" class="form-horizontal" action="${basepath}/rest/manage/catalog/insertJson" method="post">
        <input type="hidden" class="input-normal control-text" name="id"/>

        <div class="row">
            <div class="control-group span16">
                <label class="control-label">分类名称：</label>
                <div class="controls">
                    <input type="text" class="input-normal control-text" name="name" data-rules="{required : true}">
                </div>
            </div>
        </div>
        <div class="row">
            <div class="control-group span16">
                <label class="control-label">图片地址：</label>
                <div class="control-group-img controls">
                    <input type="text" class="input-normal control-text" name="icon" data-rules="{required : true}"
                           readonly>
                    <a id="addImgBtn" class="button button-min button-primary pull-right"
                       style="height:20px;margin-left: 10px; margin-top: 0.4rem;">上传</a>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="control-group span16">
                <label class="control-label">排序：</label>
                <div class="controls">
                    <input type="text" class="input-normal control-text" name="sort"
                           data-rules="{required : true,number:true}">
                </div>
            </div>
        </div>
        <div class="row">
            <div class="control-group span16">
                <label class="control-label">是否显示：</label>
                <div class="controls control-row4">
                    <select data-rules="{required:true}" name="showInNav">
                        <option value="">-请选择-</option>
                        <option value="y">显示</option>
                        <option value="n">不显示</option>
                    </select>
                </div>
            </div>
        </div>
    </form>
</div>
<#----------------------------------------------新增表单 end---------------------------------------------------------->
<#----------------------------------------------编辑表单start--------------------------------------------------------->
<div id="editContent" style="display:none;">
    <form id="editForm" class="form-horizontal" action="${basepath}/rest/manage/catalog/updateJson" method="post">
        <input type="hidden" class="input-normal control-text" name="id"/>

        <div class="row">
            <div class="control-group span16">
                <label class="control-label">分类名称：</label>

                <div class="controls">
                    <input type="text" class="input-normal control-text" name="name" data-rules="{required : true}">
                </div>
            </div>
        </div>
        <div class="row">
        <div class="control-group span16">
            <label class="control-label">图片地址：</label>
            <div class="control-group-img controls">
                <input type="text" class="input-normal control-text" name="icon" data-rules="{required : true}"
                       readonly>
                <a id="editImgBtn" class="button button-min button-primary pull-right"
                   style="height:20px;margin-left: 10px; margin-top: 0.4rem;">上传</a>
            </div>
        </div>
        </div>
        <div class="row">
            <div class="control-group span16">
                <label class="control-label">排序：</label>

                <div class="controls">
                    <input type="text" class="input-normal control-text" name="sort" id="order"
                           data-rules="{required : true,number:true}">
                </div>
            </div>
        </div>
        <div class="row">
            <div class="control-group span16">
                <label class="control-label">是否显示：</label>

                <div class="controls control-row4">
                    <select data-rules="{required:true}" name="showInNav">
                        <option value="">-请选择-</option>
                        <option value="y">显示</option>
                        <option value="n">不显示</option>
                    </select>
                </div>
            </div>
        </div>
    </form>
</div>

<div id="grid"></div>
<script>
    var pName = new Array();
    <#----------------------------------------------列表处理---------------------------------------------------------->
    var Grid = BUI.Grid,
            Store = BUI.Data.Store,
            columns = [
                {title: '序号', dataIndex: 'id', elCls: 'center', width: '10%'},
                {title: '分类名称', dataIndex: 'name', elCls: 'center', width: '30%'},
                {title: '排序', dataIndex: 'sort', elCls: 'center', width: '10%'},
                {title: '图片', dataIndex: 'icon', elCls: 'center', width: '20%',renderer:function (value) {
                        return '<img src="/' + value + '" style="width:110px;height:110px"/>';
                    }},
                {
                    title: '是否在导航栏中显示',
                    dataIndex: 'showInNav',
                    width: '20%',
                    elCls: 'center',
                    renderer: function (value) {
                      return value=='y' ? '<img src="${basepath}/resource/manage/images/action_check.gif">' : '<img src="${basepath}/resource/manage/images/action_delete.gif">'
                    }
                },
                {
                    title: '操作', dataIndex: 'id', width: '20%', elCls: 'center', renderer: function (value) {
                    return '<a href="javascript:edit(' + value + ')">编辑</a>' +
                            '<a style="margin-left:10px;" href="javascript:delOneFunction(' + value + ')">删除</a>';
                }
                }
            ];

    var store = new Store({
                url: 'loadData',
                autoLoad: true, //自动加载数据
                params: { //配置初始请求的参数
                    length: '10',
                    status: $("#status").val()
                },
                pageSize: 10,	// 配置分页数目
                root: 'list',
                totalProperty: 'total'
            }),
            grid = new Grid.Grid({
                render: '#grid',
                width: '100%',//如果表格使用百分比，这个属性一定要设置
                columns: columns,
                loadMask: true, //加载数据时显示屏蔽层
                store: store,
                plugins: [Grid.Plugins.CheckSelection], // 插件形式引入多选表格
                // 底部工具栏
                bbar: {
                    pagingBar: true
                }
            });
    grid.render();
    <#---------------------------------------------模糊查询------------------------------------>
    var form = new BUI.Form.HForm({
        srcNode: '#searchForm'
    }).render();

    form.on('beforesubmit', function (ev) {
        //序列化成对象
        var obj = form.serializeToObject();
        obj.start = 0; //返回第一页
        store.load(obj);
        return false;
    });
    <#---------------------------------------------模糊查询------------------------------------>
    /*------------------------------------------------新增表单处理start------------------------------------------------*/
    var Select = BUI.Select, Data = BUI.Data;

    var addForm = new BUI.Form.Form({
        srcNode: '#addForm',
        submitType: 'ajax',
        callback: function (data) {
            var obj = new Object();
            obj.start = 0; //返回第一页
            store.load(obj);
            addDialog.close();
        }
    }).render();

    var addDialog = new BUI.Overlay.Dialog({
        title: '新增分类',
        width: 500,
        contentId: 'addContent',
        success: function () {
            if (addForm.isValid()) {
                addForm.submit();
            } else {
                addForm.valid();
            }
        }
    });
    //添加上传图片按钮事件
    function add() {
        addForm.clearFields();
        addForm.clearErrors();      //添加的时候原来的错误提示
        addDialog.show();
        var addUploader = WebUploader.create({
            auto: true,
            swf: '${staticpath}/ueditor/third-party/webuploader/Uploader.swf',
            server: '${basepath}/rest/manage/ued/config?action=uploadimage',
            pick: '#addImgBtn',
            resize: false,
            accept: {
                title: 'Images',
                extensions: 'gif,jpg,jpeg,bmp,png',
                mimeTypes: 'image/gif,image/jpg,image/jpeg,image/bmp,image/png,'
            }
        });
        addUploader.on('uploadSuccess', function (file, response) {
            addUploader.removeFile(file);
            addForm.setFieldValue("icon", response.url)
        });
    }
    /*------------------------------------------------新增表单处理 end-------------------------------------------------*/
    /*------------------------------------------------编辑表单处理 start-----------------------------------------------*/
    var editForm = new BUI.Form.Form({
        srcNode: '#editForm',
        submitType: 'ajax',
        callback: function (data) {
            var obj = new Object();
            obj.start = 0; //返回第一页
            store.load(obj);
            editDialog.close();
        }
    });
    editForm.render();

    var editDialog = new BUI.Overlay.Dialog({
        title: '编辑分类',
        width: 500,
        contentId: 'editContent',
        success: function () {
            if (editForm.isValid()) {
                editForm.submit();
            } else {
                editForm.valid();
            }
        }
    });
    //编辑按钮事件
    function edit(id) {
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "${basepath}/rest/manage/catalog/toEditJson",
            data: {id: id},
            success: function (data) {
                var form = $("#editForm")[0];
                BUI.FormHelper.setFields(form, data.data);
                editDialog.show();
                $("#pname").val(pName[id]);
                var editUploader = WebUploader.create({
                    auto: true,
                    swf: '${staticpath}/ueditor/third-party/webuploader/Uploader.swf',
                    server: '${basepath}/rest/manage/ued/config?action=uploadimage',
                    pick: '#editImgBtn',
                    resize: false,
                    accept: {
                        title: 'Images',
                        extensions: 'gif,jpg,jpeg,bmp,png',
                        mimeTypes: 'image/gif,image/jpg,image/jpeg,image/bmp,image/png,'
                    }
                });
                editUploader.on('uploadSuccess', function (file, response) {
                    editUploader.removeFile(file);
                    editForm.setFieldValue("icon", response.url)
                });
            }
        });
    }
    /*------------------------------------------------编辑表单处理 end------------------------------------------------*/
    <#----------------------------------------------删除操作 start---------------------------------------------------------->
    //删除单个记录
    function delOneFunction(value) {
        BUI.Message.Confirm('确认要删除吗？', function () {
            $.ajax({
                type: "POST",
                url: "${basepath}/rest/manage/catalog/deleteByIdJson",
                dataType: "json",
                data: {
                    id: value
                },
                success: function (data) {
                    var obj = new Object();
                    obj.start = 0; //返回第一页
                    store.load(obj);
                }
            });
        }, 'question');
    }

    <#----------------------------------------------删除操作 end---------------------------------------------------------->
</script>

</@page.pageBase>
